<?php
/*********************/
/*                   */
/*  后台销售统计模块       */
/*   BY voiet        */
/*   2009-9-1更新    */
/*                   */
/*********************/

//require_once( CORE_DIR."/model/trading/mdl.salescount.php" );
class cmd_salescount extends mdl_salescount
{

	function count_all( $dateFrom, $dateTo, $item, $search, $order )
	{
			$ordertype = array( "1" => "saleTimes", "2" => "salePrice" );
			$method = array( "1" => "ASC", "2" => "DESC" );
			if ( is_array( $order ) )
			{
					$order = " order by ".$ordertype[$order['order']]." ".$method[$order['method']];
			}
			else
			{
					$order = " order by ".$ordertype[1]." ".$method[2];
			}
			$where = "";
			if ( $item == 'bn' )
			{
			
				$where = " and i.bn LIKE '".$search."%'";
						
			}
			$sql = "SELECT i.bn,i.name,sum(i.sendnum) as saleTimes,sum(i.sendnum*i.price) as salePrice FROM sdb_order_items as i LEFT JOIN sdb_orders as o ON i.order_id=o.order_id where o.createtime >=".$dateFrom." and o.createtime <=".$dateTo.$where." and o.disabled=\"false\" Group By i.product_id ".$order;
			return $this->db->select( $sql );
	}
	
	function mdl_dosearch( $date_From, $date_To = "", $dateCompare_From, $dateCompare_To, $type )
	{
			if ( $type == "day" )
			{
					$result = "";
					$dateFrom = strtotime( $date_From );
					if ( empty( $date_To ) || $date_To == "" )
					{
								$dateTo = strtotime( "+1 month", $dateFrom );
					}
					else
					{
								$dateTo = strtotime( $date_To ) + 86399;
					}
					$head = "<graph caption='从".date( "Y年m月d日", $dateFrom )."至".date( "Y年m月d日", $dateTo )."'>";
					$dur = ( $dateTo - $dateFrom ) / 86400;
					if ( $dur <= 0 )
					{
							return "error";
							exit( );
					}
					$sql = "SELECT P.t_end,P.money FROM sdb_payments as P \n            where P.t_end >=".$dateFrom." and P.status=\"succ\" and P.t_end <=".$dateTo." and P.disabled=\"false\" AND P.order_id!=\"NULL\" ";
					$s_from = $this->db->select( $sql );
					foreach ( $s_from as $q => $m )
					{
							$f_date[floor( ( $m['t_end'] - $dateFrom ) / 86400 )][] = $m['money'];
					}
					$i = 0;
					for ( ;	$i <= $dur;	++$i	)
					{
							$diff = strtotime( $date_From ) + $i * 86400;
							$sum = @array_sum( $f_date[$i] ) ? array_sum( $f_date[$i] ) : 0;
							$result .= "<set name='".date( "m-d", $diff )."' value='".$sum."'/>";
					}
					if ( $dateCompare_To && $dateCompare_From )
					{
							$head = "<graph caption='从".date( "Y年m月d日", strtotime( $date_From ) )."至".date( "Y年m月d日", strtotime( $date_To ) )."对比 ".date( "Y年m月d日", strtotime( $dateCompare_From ) )." 至 ".date( "Y年m月d日", strtotime( $dateCompare_To ) )." 结果' type='compare'>";
							$dateCompareTo = strtotime( $dateCompare_To );
							$dateCompareFrom = strtotime( $dateCompare_From );
							$dur2 = ( strtotime( $dateCompare_To ) - strtotime( $dateCompare_From ) ) / 86400;
							if ( $dur2 <= 0 )
							{
										return "error";
										exit( );
							}
							$sql = "SELECT P.t_end,P.money FROM sdb_payments as P \n                where P.t_end >=".$dateCompareFrom." and P.status=\"succ\" AND P.order_id!=\"NULL\" and P.t_end <=".$dateCompareTo;
							$c_from = $this->db->select( $sql );
							foreach ( $c_from as $q => $m )
							{
									$t_date[floor( ( $m['t_end'] - $dateCompareFrom ) / 86400 )][] = $m['money'];
							}
							$i = 0;
							for ( ;	$i <= $dur2;	++$i	)
							{
									$diff = strtotime( $dateCompare_From ) + $i * 86400;
									$sum = @array_sum( $t_date[$i] ) ? array_sum( $t_date[$i] ) : 0;
									$result .= "<compared name='".date( "m-d", $diff )."' value='".$sum."'/>";
							}
					}
					$result = $head.$result."</graph>";
				}
				if ( $type == "month" )
				{
					$result = "";
					$head = "<graph caption='从".date( "Y年m月", strtotime( $date_From ) )."至".date( "Y年m月", strtotime( $date_To ) )."'>";
					if ( empty( $date_From ) || empty( $date_To ) )
					{
							return "error";
					}
					$dateFrom = strtotime( $date_From );
					$dateTo = strtotime( $date_To );
					$dur = date( "m", $dateTo ) - date( "m", $dateFrom ) + ( date( "Y", $dateTo ) - date( "Y", $dateFrom ) ) * 12;
					if ( $dur <= 0 )
					{
							return "error";
					}
					$sql = "SELECT P.t_end,P.money FROM sdb_payments as P \n            where P.t_end >=".$dateFrom." and P.status=\"succ\" AND P.order_id!=\"NULL\" and P.t_end <=".$dateTo." and P.disabled=\"false\"";
							$s_from = $this->db->select( $sql );
							foreach ( $s_from as $q => $m )
							{
									$f_date[date( "Y-m", $m['t_end'] )][] = $m['money'];
							}
							$i = 0;
							for ( ;	$i <= $dur;	++$i	)
							{
									$diff = strtotime( "+".$i." month", $dateFrom );
									$sum = @array_sum( $f_date[date( "Y-m", $diff )] ) ? array_sum( $f_date[date( "Y-m", $diff )] ) : 0;
									$result .= "<set name='".date( "y-m", $diff )."' value='".$sum."'/>";
							}
							if ( $dateCompare_To && $dateCompare_From )
							{
									$head = "<graph caption='从".date( "Y年m月", strtotime( $date_From ) )."至".date( "Y年m月", strtotime( $date_To ) )."对比 ".date( "Y年m月", strtotime( $dateCompare_From ) )." 至 ".date( "Y年m月", strtotime( $dateCompare_To ) )." 结果' type='compare'>";
									$dateCompareTo = strtotime( $dateCompare_To );
									$dateCompareFrom = strtotime( $dateCompare_From );
									$dur2 = date( "m", $dateCompareTo ) - date( "m", $dateCompareFrom ) + ( date( "Y", $dateCompareTo ) - date( "Y", $dateCompareFrom ) ) * 12;
									if ( $dur2 <= 0 )
									{
											return "error";
									}
									$sql = "SELECT P.t_end,P.money FROM sdb_payments as P \n                where P.t_end >=".$dateCompareFrom." and P.status=\"succ\" AND P.order_id!=\"NULL\" and P.t_end <=".$dateCompareTo;
									$c_from = $this->db->select( $sql );
									foreach ( $c_from as $q => $m )
									{
											$t_date[date( "y-m", $m['t_end'] )][] = $m['money'];
									}
									$i = 0;
									for ( ;	$i <= $dur2;	++$i	)
									{
											$diff = strtotime( "+".$i." month", $dateCompareFrom );
											$sum = @array_sum( $t_date[date( "Y-m", $diff )] ) ? array_sum( $t_date[date( "Y-m", $diff )] ) : 0;
											$result .= "<compared name='".date( "y-m", $diff )."' value='".$sum."'/>";
									}
							}
							$result = $head.$result."</graph>";
					}
					return $result;
	}
	
	function member_count( $dateFrom, $dateTo, $item, $search, $order )
	{
			$ordertype = array( "1" => "saleTimes", "2" => "salePrice" );
			$method = array( "1" => "ASC", "2" => "DESC" );
			if ( is_array( $order ) )
			{
					$order = " order by ".$ordertype[$order['order']]." ".$method[$order['method']];
			}
			else
			{
					$order = " order by ".$ordertype[1]." ".$method[2];
			}
			$where = "";
			if ( $item == 'username' )
			{
			
				$where = " and M.uname = '".$search."'";
						
			}
			$sql = "SELECT  M.regtime, M.uname as username , M.name as realname,sum(I.sendnum) as saleTimes,sum(I.sendnum*price) as salePrice FROM  sdb_order_items as I LEFT JOIN sdb_orders as O on I.order_id=O.order_id \n  LEFT JOIN sdb_members as M on O.member_id=M.member_id\n where O.createtime >=".$dateFrom." and O.createtime <=".$dateTo.$where." Group By M.member_id".$order;
			return $this->db->select( $sql );
	}
	
	function memberreg_count( $dateFrom, $dateTo, $order )
	{
			$ordertype = array( "byhour" => "from_unixtime(regtime,'%H')", "byday" => "from_unixtime(regtime,'%Y-%m-%d')" , "bymonth" => "from_unixtime(regtime,'%Y-%m')", "refer_id" => "refer_id", "refer_url" => "refer_url", "b_year" => "b_year", "area" => "area");
			if ( $order )
			{
					$select = $ordertype[$order];
			}
			else
			{
					$select = "from_unixtime(regtime,'%H')";
			}			
			$where = "";
			
			$sql = "SELECT  ".$select." AS mydate, count(member_id) as nums FROM  sdb_members   where regtime >=".$dateFrom." and regtime <=".$dateTo.$where." Group By mydate Order By mydate";
			return $this->db->select( $sql );
	}
	
	function order_count( $dateFrom, $dateTo, $order )
	{
			$ordertype = array( "byhour" => "from_unixtime(createtime,'%H')", "byday" => "from_unixtime(createtime,'%Y-%m-%d')" , "bymonth" => "from_unixtime(createtime,'%Y-%m')", "refer_id" => "refer_id", "refer_url" => "refer_url", "shipping" => "shipping", "itemnum" => "itemnum", "ship_area" => "ship_area", "member_lv_id" => "member_lv_id", "member_id" => "member_id", "payment" => "payment");
			if ( $order )
			{
					$select = $ordertype[$order];
			}
			else
			{
					$select = "from_unixtime(createtime,'%H')";
			}			
			$where = "";
			
			$sql = "SELECT  ".$select." AS mydate, count(order_id) as nums, sum(cost_item) as cost_item FROM  sdb_orders where createtime >=".$dateFrom." and createtime <=".$dateTo.$where." Group By mydate Order By mydate";
			return $this->db->select( $sql );
	}

}

?>